/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_layer_light.h
 * Desc:    Mesh layer light shader
 * Version: 1.02
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	#ifndef FRAGMENT_LAYER_LIGHT
		#define FRAGMENT_LAYER_LIGHT
		
		uniform sampler2D s_texture_0;
		uniform sampler2D s_texture_1;
		uniform sampler2D s_texture_2;
		uniform sampler2D s_texture_3;
		uniform sampler2D s_texture_4;
		uniform sampler2D s_texture_5;
		uniform sampler2D s_texture_6;
		
		uniform half4 mask_transform;
		uniform half mask_value;
		uniform half mask_threshold;
		
		uniform half4 layer_0_transform;
		uniform half4 diffuse_0_color;
		uniform half4 specular_0_color;
		uniform half diffuse_0_scale;
		uniform half specular_0_scale;
		uniform half specular_0_power;
		uniform half phong_rim_0_width;
		uniform half phong_rim_0_scale;
		uniform half fresnel_0_bias;
		uniform half fresnel_0_power;
		
		uniform half4 layer_1_transform;
		uniform half4 diffuse_1_color;
		uniform half4 specular_1_color;
		uniform half diffuse_1_scale;
		uniform half specular_1_scale;
		uniform half specular_1_power;
		uniform half phong_rim_1_width;
		uniform half phong_rim_1_scale;
		uniform half fresnel_1_bias;
		uniform half fresnel_1_power;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_layer_sample.h>
		
		half3 camera_direction = s_texcoord_2.xyz;
		
		half3 material_shading_0 = s_material_shading * half3(diffuse_0_scale,specular_0_scale,specular_0_power);
		half3 material_shading_1 = s_material_shading * half3(diffuse_1_scale,specular_1_scale,specular_1_power);
		
		half3 fresnel_0 = half3(fresnel_0_bias,1.0f - fresnel_0_bias,fresnel_0_power);
		half3 fresnel_1 = half3(fresnel_1_bias,1.0f - fresnel_1_bias,fresnel_1_power);
		
		#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
			half2 material_phong_rim_0 = half2(phong_rim_0_width,phong_rim_0_scale);
			half2 material_phong_rim_1 = half2(phong_rim_1_width,phong_rim_1_scale);
		#endif
		
		#ifdef WORLD
			half3 light_direction = s_texcoord_1.xyz;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 color_0 = getDiffuseRim(material_shading_0,material_phong_rim_0,diffuse_0.xyz,normal_0,light_direction);
				half3 color_1 = getDiffuseRim(material_shading_1,material_phong_rim_1,diffuse_1.xyz,normal_1,light_direction);
			#else
				half3 color_0 = getDiffuse(material_shading_0,diffuse_0.xyz,normal_0,light_direction);
				half3 color_1 = getDiffuse(material_shading_1,diffuse_1.xyz,normal_1,light_direction);
			#endif
			color_0 += getSpecularFresnelNormalize(material_shading_0,fresnel_0,specular_0,normal_0,light_direction,camera_direction);
			color_1 += getSpecularFresnelNormalize(material_shading_1,fresnel_1,specular_1,normal_1,light_direction,camera_direction);
		#else
			half4 light_direction = s_texcoord_1;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 color_0 = getDiffuseRimSpecularFresnelNormalizeAttenuate(material_shading_0,material_phong_rim_0,fresnel_0,diffuse_0.xyz,specular_0,normal_0,light_direction,camera_direction);
				half3 color_1 = getDiffuseRimSpecularFresnelNormalizeAttenuate(material_shading_1,material_phong_rim_1,fresnel_1,diffuse_1.xyz,specular_1,normal_1,light_direction,camera_direction);
			#else
				half3 color_0 = getDiffuseSpecularFresnelNormalizeAttenuate(material_shading_0,fresnel_0,diffuse_0.xyz,specular_0,normal_0,light_direction,camera_direction);
				half3 color_1 = getDiffuseSpecularFresnelNormalizeAttenuate(material_shading_1,fresnel_1,diffuse_1.xyz,specular_1,normal_1,light_direction,camera_direction);
			#endif
		#endif
		
		half3 color = lerp(color_0,color_1,layer);
		
	#endif
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	#ifndef FRAGMENT_LAYER_LIGHT
		#define FRAGMENT_LAYER_LIGHT
		
		Texture2D s_texture_0 : register(t0);
		Texture2D s_texture_1 : register(t1);
		Texture2D s_texture_2 : register(t2);
		Texture2D s_texture_3 : register(t3);
		Texture2D s_texture_4 : register(t4);
		Texture2D s_texture_5 : register(t5);
		Texture2D s_texture_6 : register(t6);
		
		cbuffer shader_parameters {
			float4 mask_transform;
			float mask_value;
			float mask_threshold;
			float4 layer_0_transform;
			float4 diffuse_0_color;
			float4 specular_0_color;
			float diffuse_0_scale;
			float specular_0_scale;
			float specular_0_power;
			float phong_rim_0_width;
			float phong_rim_0_scale;
			float fresnel_0_bias;
			float fresnel_0_power;
			float4 layer_1_transform;
			float4 diffuse_1_color;
			float4 specular_1_color;
			float diffuse_1_scale;
			float specular_1_scale;
			float specular_1_power;
			float phong_rim_1_width;
			float phong_rim_1_scale;
			float fresnel_1_bias;
			float fresnel_1_power;
		};
		
	#else
		
		#include <core/shaders/default/mesh/fragment_layer_sample.h>
		
		half3 camera_direction = IN.texcoord_2.xyz;
		
		half3 material_shading_0 = s_material_shading * half3(diffuse_0_scale,specular_0_scale,specular_0_power);
		half3 material_shading_1 = s_material_shading * half3(diffuse_1_scale,specular_1_scale,specular_1_power);
		
		half3 fresnel_0 = half3(fresnel_0_bias,1.0f - fresnel_0_bias,fresnel_0_power);
		half3 fresnel_1 = half3(fresnel_1_bias,1.0f - fresnel_1_bias,fresnel_1_power);
		
		#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
			half2 material_phong_rim_0 = half2(phong_rim_0_width,phong_rim_0_scale);
			half2 material_phong_rim_1 = half2(phong_rim_1_width,phong_rim_1_scale);
		#endif
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 color_0 = getDiffuseRim(material_shading_0,material_phong_rim_0,diffuse_0.xyz,normal_0,light_direction);
				half3 color_1 = getDiffuseRim(material_shading_1,material_phong_rim_1,diffuse_1.xyz,normal_1,light_direction);
			#else
				half3 color_0 = getDiffuse(material_shading_0,diffuse_0.xyz,normal_0,light_direction);
				half3 color_1 = getDiffuse(material_shading_1,diffuse_1.xyz,normal_1,light_direction);
			#endif
			color_0 += getSpecularFresnelNormalize(material_shading_0,fresnel_0,specular_0,normal_0,light_direction,camera_direction);
			color_1 += getSpecularFresnelNormalize(material_shading_1,fresnel_1,specular_1,normal_1,light_direction,camera_direction);
		#else
			half4 light_direction = IN.texcoord_1;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 color_0 = getDiffuseRimSpecularFresnelNormalizeAttenuate(material_shading_0,material_phong_rim_0,fresnel_0,diffuse_0.xyz,specular_0,normal_0,light_direction,camera_direction);
				half3 color_1 = getDiffuseRimSpecularFresnelNormalizeAttenuate(material_shading_1,material_phong_rim_1,fresnel_1,diffuse_1.xyz,specular_1,normal_1,light_direction,camera_direction);
			#else
				half3 color_0 = getDiffuseSpecularFresnelNormalizeAttenuate(material_shading_0,fresnel_0,diffuse_0.xyz,specular_0,normal_0,light_direction,camera_direction);
				half3 color_1 = getDiffuseSpecularFresnelNormalizeAttenuate(material_shading_1,fresnel_1,diffuse_1.xyz,specular_1,normal_1,light_direction,camera_direction);
			#endif
		#endif
		
		half3 color = lerp(color_0,color_1,layer);
		
	#endif
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	#ifndef FRAGMENT_LAYER_LIGHT
		#define FRAGMENT_LAYER_LIGHT
		
		half4 mask_transform;
		half mask_value;
		half mask_threshold;
		
		half4 layer_0_transform;
		half4 diffuse_0_color;
		half4 specular_0_color;
		half diffuse_0_scale;
		half specular_0_scale;
		half specular_0_power;
		half phong_rim_0_width;
		half phong_rim_0_scale;
		half fresnel_0_bias;
		half fresnel_0_power;
		
		half4 layer_1_transform;
		half4 diffuse_1_color;
		half4 specular_1_color;
		half diffuse_1_scale;
		half specular_1_scale;
		half specular_1_power;
		half phong_rim_1_width;
		half phong_rim_1_scale;
		half fresnel_1_bias;
		half fresnel_1_power;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_layer_sample.h>
		
		half3 camera_direction = IN.texcoord_2.xyz;
		
		half3 material_shading_0 = s_material_shading * half3(diffuse_0_scale,specular_0_scale,specular_0_power);
		half3 material_shading_1 = s_material_shading * half3(diffuse_1_scale,specular_1_scale,specular_1_power);
		
		half3 fresnel_0 = half3(fresnel_0_bias,1.0f - fresnel_0_bias,fresnel_0_power);
		half3 fresnel_1 = half3(fresnel_1_bias,1.0f - fresnel_1_bias,fresnel_1_power);
		
		#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
			half2 material_phong_rim_0 = half2(phong_rim_0_width,phong_rim_0_scale);
			half2 material_phong_rim_1 = half2(phong_rim_1_width,phong_rim_1_scale);
		#endif
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 color_0 = getDiffuseRim(material_shading_0,material_phong_rim_0,diffuse_0.xyz,normal_0,light_direction);
				half3 color_1 = getDiffuseRim(material_shading_1,material_phong_rim_1,diffuse_1.xyz,normal_1,light_direction);
			#else
				half3 color_0 = getDiffuse(material_shading_0,diffuse_0.xyz,normal_0,light_direction);
				half3 color_1 = getDiffuse(material_shading_1,diffuse_1.xyz,normal_1,light_direction);
			#endif
			color_0 += getSpecularFresnelNormalize(material_shading_0,fresnel_0,specular_0,normal_0,light_direction,camera_direction);
			color_1 += getSpecularFresnelNormalize(material_shading_1,fresnel_1,specular_1,normal_1,light_direction,camera_direction);
		#else
			half4 light_direction = IN.texcoord_1;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 color_0 = getDiffuseRimSpecularFresnelNormalizeAttenuate(material_shading_0,material_phong_rim_0,fresnel_0,diffuse_0.xyz,specular_0,normal_0,light_direction,camera_direction);
				half3 color_1 = getDiffuseRimSpecularFresnelNormalizeAttenuate(material_shading_1,material_phong_rim_1,fresnel_1,diffuse_1.xyz,specular_1,normal_1,light_direction,camera_direction);
			#else
				half3 color_0 = getDiffuseSpecularFresnelNormalizeAttenuate(material_shading_0,fresnel_0,diffuse_0.xyz,specular_0,normal_0,light_direction,camera_direction);
				half3 color_1 = getDiffuseSpecularFresnelNormalizeAttenuate(material_shading_1,fresnel_1,diffuse_1.xyz,specular_1,normal_1,light_direction,camera_direction);
			#endif
		#endif
		
		half3 color = lerp(color_0,color_1,layer);
		
	#endif
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	#ifndef FRAGMENT_LAYER_LIGHT
		#define FRAGMENT_LAYER_LIGHT
		
		uniform sampler2D s_texture_0 : TEXUNIT0;
		uniform sampler2D s_texture_1 : TEXUNIT1;
		uniform sampler2D s_texture_2 : TEXUNIT2;
		uniform sampler2D s_texture_3 : TEXUNIT3;
		uniform sampler2D s_texture_4 : TEXUNIT4;
		uniform sampler2D s_texture_5 : TEXUNIT5;
		uniform sampler2D s_texture_6 : TEXUNIT6;
		
		uniform half4 mask_transform;
		uniform half mask_value;
		uniform half mask_threshold;
		
		uniform half4 layer_0_transform;
		uniform half4 diffuse_0_color;
		uniform half4 specular_0_color;
		uniform half diffuse_0_scale;
		uniform half specular_0_scale;
		uniform half specular_0_power;
		uniform half phong_rim_0_width;
		uniform half phong_rim_0_scale;
		uniform half fresnel_0_bias;
		uniform half fresnel_0_power;
		
		uniform half4 layer_1_transform;
		uniform half4 diffuse_1_color;
		uniform half4 specular_1_color;
		uniform half diffuse_1_scale;
		uniform half specular_1_scale;
		uniform half specular_1_power;
		uniform half phong_rim_1_width;
		uniform half phong_rim_1_scale;
		uniform half fresnel_1_bias;
		uniform half fresnel_1_power;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_layer_sample.h>
		
		half3 camera_direction = IN.texcoord_2.xyz;
		
		half3 material_shading_0 = s_material_shading * half3(diffuse_0_scale,specular_0_scale,specular_0_power);
		half3 material_shading_1 = s_material_shading * half3(diffuse_1_scale,specular_1_scale,specular_1_power);
		
		half3 fresnel_0 = half3(fresnel_0_bias,1.0f - fresnel_0_bias,fresnel_0_power);
		half3 fresnel_1 = half3(fresnel_1_bias,1.0f - fresnel_1_bias,fresnel_1_power);
		
		#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
			half2 material_phong_rim_0 = half2(phong_rim_0_width,phong_rim_0_scale);
			half2 material_phong_rim_1 = half2(phong_rim_1_width,phong_rim_1_scale);
		#endif
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 color_0 = getDiffuseRim(material_shading_0,material_phong_rim_0,diffuse_0.xyz,normal_0,light_direction);
				half3 color_1 = getDiffuseRim(material_shading_1,material_phong_rim_1,diffuse_1.xyz,normal_1,light_direction);
			#else
				half3 color_0 = getDiffuse(material_shading_0,diffuse_0.xyz,normal_0,light_direction);
				half3 color_1 = getDiffuse(material_shading_1,diffuse_1.xyz,normal_1,light_direction);
			#endif
			color_0 += getSpecularFresnelNormalize(material_shading_0,fresnel_0,specular_0,normal_0,light_direction,camera_direction);
			color_1 += getSpecularFresnelNormalize(material_shading_1,fresnel_1,specular_1,normal_1,light_direction,camera_direction);
		#else
			half4 light_direction = IN.texcoord_1;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 color_0 = getDiffuseRimSpecularFresnelNormalizeAttenuate(material_shading_0,material_phong_rim_0,fresnel_0,diffuse_0.xyz,specular_0,normal_0,light_direction,camera_direction);
				half3 color_1 = getDiffuseRimSpecularFresnelNormalizeAttenuate(material_shading_1,material_phong_rim_1,fresnel_1,diffuse_1.xyz,specular_1,normal_1,light_direction,camera_direction);
			#else
				half3 color_0 = getDiffuseSpecularFresnelNormalizeAttenuate(material_shading_0,fresnel_0,diffuse_0.xyz,specular_0,normal_0,light_direction,camera_direction);
				half3 color_1 = getDiffuseSpecularFresnelNormalizeAttenuate(material_shading_1,fresnel_1,diffuse_1.xyz,specular_1,normal_1,light_direction,camera_direction);
			#endif
		#endif
		
		half3 color = lerp(color_0,color_1,layer);
		
	#endif
	
#endif
